package jeecg.business.controller.alarm;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.ExceptionUtil;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.excel.ExcelUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import jeecg.business.entity.alarm.AlarmWordEntity;
import jeecg.business.service.alarm.AlarmWordServiceI;
import jeecg.system.service.SystemService;

/**   
 * @Title: Controller
 * @Description: 警戒词
 * @author LinYingPiaoJian
 * @date 2015-06-08 05:40:15
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/alarmWordController")
public class AlarmWordController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(AlarmWordController.class);

	@Autowired
	private AlarmWordServiceI alarmWordService;
	@Autowired
	private SystemService systemService;
	
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 警戒词列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "alarmWord")
	public ModelAndView alarmWord(HttpServletRequest request) {
		return new ModelAndView("jeecg/business/alarm/alarmWordList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(AlarmWordEntity alarmWord,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(AlarmWordEntity.class, dataGrid);
		//查询条件组装器
		if(alarmWord != null){
			if(StringUtil.isNotEmpty(alarmWord.getCy()))
				alarmWord.setCy("*"+alarmWord.getCy()+"*");
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, alarmWord);
		this.alarmWordService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除警戒词
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(AlarmWordEntity alarmWord, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		alarmWord = systemService.getEntity(AlarmWordEntity.class, alarmWord.getId());
		message = "删除成功";
		alarmWordService.delete(alarmWord);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加警戒词
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(AlarmWordEntity alarmWord, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(alarmWord.getId())) {
			message = "更新成功";
			AlarmWordEntity t = alarmWordService.get(AlarmWordEntity.class, alarmWord.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(alarmWord, t);
				alarmWordService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			alarmWordService.save(alarmWord);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 警戒词列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(AlarmWordEntity alarmWord, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(alarmWord.getId())) {
			alarmWord = alarmWordService.getEntity(AlarmWordEntity.class, alarmWord.getId());
		}else{
			alarmWord = new AlarmWordEntity();
			alarmWord.setQzz(1.0f);
			alarmWord.setSxh(999);
		}
		req.setAttribute("alarmWordPage", alarmWord);
		return new ModelAndView("jeecg/business/alarm/alarmWord");
	}
	
	@RequestMapping(params = "goImplXls")
    public ModelAndView goImplXls(HttpServletRequest request){
	    return  new ModelAndView("jeecg/business/alarm/alarm_upload");
    }
	
	
	@RequestMapping(params = "implXls")
	@ResponseBody
    public AjaxJson implXls(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			List<AlarmWordEntity> alarmList;
	        //-- update-begin--Author:lihuan  Date:20130509 for：增加判断格式不正确的情况，防止格式不正确时抛出异常到页面 
			try {
				boolean isSuccess = true;
				alarmList = (List<AlarmWordEntity>) ExcelUtil.importExcelByIs(file.getInputStream(), AlarmWordEntity.class);
				
				for (AlarmWordEntity alarm : alarmList) {
					alarm.setSxh(999);
				}
				alarmWordService.importXls(alarmList);
				
				if (isSuccess)
					j.setMsg("导入成功！");
				else
					j.setMsg("导入失败！");
			} catch (Exception e) {
				e.printStackTrace();
	            j.setMsg("导入失败"+e.getMessage());            
	            logger.error(ExceptionUtil.getExceptionMessage(e));     
			} 
		}
		//-- update-begin--Author:lihuan  Date:20130509 for：增加判断格式不正确的情况，防止格式不正确时抛出异常到页面 
        return j;
    }
}
